package com.nut.lmsc.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.SysDict;
import com.nut.lmsc.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dict")
public class DictController {

    @Autowired
    private SysDictService sysDictService;

    @GetMapping(value = "/list")
    public Result<IPage<SysDict>> list(@RequestParam(required = false) String name,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return sysDictService.list(name, pageNo, pageSize);
    }

    @GetMapping(value = "/lazy")
    public Result<List<SysDict>> lazy(@RequestParam(name = "pid", defaultValue = "0") Integer pid) {
        List<SysDict> list = sysDictService.getByPid(pid);
        return Result.ok(list);
    }

    @GetMapping(value = "/{id}")
    public Result<SysDict> detail(@PathVariable Integer id) {
        SysDict sysDict = sysDictService.getById(id);
        return Result.ok(sysDict);
    }

    @PostMapping
    public Result add(@RequestBody SysDict sysDict) {
        sysDictService.save(sysDict);
        return Result.ok();
    }

    @PutMapping
    public Result update(@RequestBody SysDict sysDict) {
        sysDictService.updateById(sysDict);
        return Result.ok();
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        sysDictService.deleteByIds(ids);
        return Result.ok();
    }

}
